package com.clkj.tramcarlibrary.http;


import com.google.gson.JsonObject;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.PartMap;
import retrofit2.http.Query;
import rx.Observable;


/**
 * 网络服务
 * Created by Administrator on 2016/10/25.
 */
public interface HttpService {


    /**
     * 获取周边的taxi信息
     *
     * @param lonValue
     * @param latValue
     * @param aroundRadius
     * @return
     */
    @GET("webservice.php")
    Observable<JsonObject> getAroundTaxiInfo(
            @Query("lon") String lonValue,
            @Query("lat") String latValue,
            @Query("radius") String aroundRadius
    );


    @GET("index.php?g=portal&m=webservice&a=ygdc_zdzb")
    Observable<JsonObject> getTramCarInfoList(
    );


    /**
     * 获取有轨电车周边的站点信息
     *
     * @return
     */
    @GET("index.php?g=portal&m=webservice&a=ygdc_zdzb")
    Observable<JsonObject> getTramcarStops();

    /**
     * 获取站点详情
     *
     * @return
     */
    @GET("index.php?g=portal&m=webservice&a=ygdc_time_one")
    Observable<JsonObject> getStopDetail(@Query("station_name") String station_name);

    /**
     * 获取南门进站详情
     *
     * @return
     */
    @GET("index.php?g=portal&m=webservice&a=ygdc_nm_time")
    Observable<JsonObject> getToNmJinzhanDetail();

    /**
     * 获取南门首末班详情
     *
     * @return
     */
    @GET("index.php?g=portal&m=webservice&a=ygdc_fltime_nm")
    Observable<JsonObject> getToNmShoumoDetail();

    /**
     * 获取体育馆进站详情
     *
     * @return
     */
    @GET("index.php?g=portal&m=webservice&a=ygdc_tyg_time")
    Observable<JsonObject> getToTygJinzhanDetail();

    /**
     * 获取体育馆首末班详情
     *
     * @return
     */
    @GET("index.php?g=portal&m=webservice&a=ygdc_fltime_tyg")
    Observable<JsonObject> getToTygShoumoDetail();

    //获取距离定位坐标最近的一个站点信息和距离
    @GET("index.php?g=portal&m=webservice&a=zj_zdjl")
    Observable<JsonObject> getRecentStop(
            @Query("lat") String latValue,
            @Query("lon") String lonValue);

    //获取所有站点的信息
    @GET("index.php?g=portal&m=webservice&a=distance")
    Observable<JsonObject> getAllDistance(
            @Query("lat") String lat,
            @Query("lon") String lon);

    /*
        g=portal&m=webservice&a=ygdc_zdzb
    */
    //获取公告新闻列表
    @GET("index.php?g=portal&m=webservice&a=zxgg")
    Observable<JsonObject> getNewsList(
            @Query("page") String page,
            @Query("pagenum") String pagenum);

    //获取轮播图
    @GET("index.php?g=portal&m=webservice&a=zxgg_hdp")
    Observable<JsonObject> getLunboImages();

    //获取新闻详情
    @GET("index.php?g=portal&m=webservice&a=ggxq")
    Observable<JsonObject> getNewsDetail(
            @Query("article_id") String article_id
    );

    //上传意见反馈
    @Multipart
    @POST("index.php?g=portal&m=webservice&a=jyts")
    Observable<JsonObject> upSuggest(
            @Part("ktype") RequestBody ktype, @Part("content") RequestBody content, @Part("tel") RequestBody tel,
            @PartMap Map<String, RequestBody> picMaps
    );

    //获取旅游包车 车辆类型
    @GET("index.php?g=portal&m=webservice&a=cartype")
    Observable<JsonObject> getBusTypes();

    //提交包车需求http://taxiwebservice.lousw.net/index.php?g=portal&m=webservice&a=lybc
    //int xqtypwe, int bctype, String begin_place, String end_place, String begin_time, String back_time, int peoplenum,
    // String cartype, int carnum, String name, String tel, int issjcy, int issjzs, int islqfy, int isfp, String other, String carinfo

    @GET("index.php?g=portal&m=webservice&a=lybc")
    Observable<JsonObject> submitNeed(
            @Query("xqtypwe") int xqtypwe, @Query("bctype") int bctype,
            @Query("begin_place") String begin_place, @Query("end_place") String end_place,
            @Query("begin_time") String begin_time, @Query("back_time") String back_time,
            @Query("peoplenum") int peoplenum, @Query("cartype") String cartype,
            @Query("carnum") int carnum, @Query("name") String name,
            @Query("tel") String tel, @Query("issjcy") int issjcy,
            @Query("issjzs") int issjzs, @Query("islqfy") int islqfy,
            @Query("isfp") int isfp, @Query("other") String other,
            @Query("carinfo") String carinfo
    );

    class Factory {

        private static OkHttpClient generateOkHttpClient() {
            OkHttpClient httpClient = new OkHttpClient.Builder()
                    .readTimeout(5, TimeUnit.MINUTES)
                    .connectTimeout(5, TimeUnit.MINUTES)
                    .writeTimeout(5, TimeUnit.MINUTES)
                    .build();


            /*InputStream cerInputStream = null;
            HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(new InputStream[]{cerInputStream},null,null);

            OkHttpClient httpClient = new OkHttpClient.Builder()
                    .readTimeout(5, TimeUnit.MINUTES)
                    .connectTimeout(5, TimeUnit.MINUTES)
                    .writeTimeout(5, TimeUnit.MINUTES)
                    .sslSocketFactory(sslParams.sSLSocketFactory,sslParams.trustManager)
                    .build();*/


            return httpClient;
        }

        public static HttpService create(String baseUrl) {

            /*Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();*/

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl + "/")
                    .client(generateOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
            return retrofit.create(HttpService.class);
        }
    }
}
